package org.jeecg.modules.fw.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.entity.BusMemberCard;
import org.jeecg.modules.fw.dto.RoomRepairDto;
import org.jeecg.modules.fw.entity.FwRoomRepair;
import org.jeecg.modules.fw.service.IFwRoomRepairService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.rooms.Enum.RoomStatusEnum;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.service.CesRoomsServiceImpl;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: fw_room_repair
 * @Author: jeecg-boot
 * @Date:   2023-04-17
 * @Version: V1.0
 */
@Api(tags="fw_room_repair")
@RestController
@RequestMapping("/fw/fwRoomRepair")
@Slf4j
public class FwRoomRepairController extends JeecgController<FwRoomRepair, IFwRoomRepairService> {
	@Autowired
	private IFwRoomRepairService fwRoomRepairService;
	 @Autowired
	 private CesRoomsServiceImpl cesRoomsService;
	/**
	 * 分页列表查询
	 *
	 * @param fwRoomRepair
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "fw_room_repair-分页列表查询")
	@ApiOperation(value="fw_room_repair-分页列表查询", notes="fw_room_repair-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FwRoomRepair>> queryPageList(FwRoomRepair fwRoomRepair,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													 @RequestParam(name="startTime",required = false) String startTime,
													 @RequestParam(name="endTime",required = false) String endTime,
								   HttpServletRequest req) {
		LambdaQueryWrapper<FwRoomRepair> queryWrapper = QueryGenerator.initQueryWrapper(fwRoomRepair, req.getParameterMap()).lambda();
		if (ObjectUtils.isNotEmpty(startTime)&&ObjectUtils.isNotEmpty(endTime)) {
			queryWrapper.and(j -> {
				j.ge(FwRoomRepair::getStartDate, DateUtil.parseDate(startTime));
				j.le(FwRoomRepair::getStartDate, DateUtil.parseDate(startTime));
			}).or().and(i->{
				i.le(FwRoomRepair::getStartDate, DateUtil.parseDate(endTime));
				i.ge(FwRoomRepair::getStartDate, DateUtil.parseDate(endTime));
			});
		}
		Page<FwRoomRepair> page = new Page<FwRoomRepair>(pageNo, pageSize);
		IPage<FwRoomRepair> pageList = fwRoomRepairService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "fw_room_repair-添加")
	@ApiOperation(value="fw_room_repair-添加", notes="fw_room_repair-添加")
	//@RequiresPermissions("fw:fw_room_repair:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoomRepairDto dto) {
		if (ObjectUtils.isEmpty(dto.getRoomRepairList())) {
			return Result.error("请先选择房间");
		}
		for (FwRoomRepair fwRoomRepair : dto.getRoomRepairList()) {
			CesRooms cesRooms = cesRoomsService.getById(fwRoomRepair.getRoomId());
			if (ObjectUtils.isEmpty(cesRooms)) {
				return Result.error("房间不存在");
			}
			if (cesRooms.getRoomStatus().equals(RoomStatusEnum.LIVE_CLEAR.getKey()) || cesRooms.getRoomStatus().equals(RoomStatusEnum.LIVE_DIRTY.getKey())) {
				return Result.error("房间在住状态不可以维修");
			}
			fwRoomRepair.setHotelId(dto.getHotelId());
			fwRoomRepair.setTenantId(TokenUtils.currentTenantId());
			fwRoomRepair.setRemark(dto.getRemark());
			fwRoomRepair.setStartDate(dto.getStartDate());
			fwRoomRepair.setEndDate(dto.getEndDate());
		}
		Boolean res = fwRoomRepairService.saveBatch(dto.getRoomRepairList());
		if (res) {
			for (FwRoomRepair fwRoomRepair : dto.getRoomRepairList()) {
//				LambdaUpdateWrapper<CesRooms> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//				lambdaUpdateWrapper.eq(CesRooms::getId, fwRoomRepair.getRoomId());
//				lambdaUpdateWrapper.set(CesRooms::getRoomStatus, RoomStatusEnum.IN_SERVICE.getKey());
//				cesRoomsService.update(lambdaUpdateWrapper);
				CesRooms cesRooms = cesRoomsService.getById(fwRoomRepair.getRoomId());
				if (ObjectUtils.isNotEmpty(cesRooms)) {
					cesRooms.setLastRoomStatus(cesRooms.getRoomStatus());
					cesRooms.setRoomStatus(RoomStatusEnum.IN_SERVICE.getKey());
					cesRoomsService.updateById(cesRooms);
				}
			}
		}
		return Result.OK("添加成功！");
	}

	 /**
	  * 房间完成维修
	  * @param roomIds
	  * @return
	  */
	 @AutoLog(value = "fw_room_lock-房间完成维修")
	 @ApiOperation(value="fw_room_lock-房间完成维修", notes="fw_room_lock-房间完成维修")
	 @PostMapping(value = "/completeRepairRoom")
	 public Result<String> completeRepairRoom(@RequestParam(name="roomIds",required=true) String roomIds) {
		 LambdaUpdateWrapper<FwRoomRepair> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		 lambdaUpdateWrapper.in(FwRoomRepair::getRoomId, Arrays.asList(roomIds.split(",")));
		 lambdaUpdateWrapper.set(FwRoomRepair::getStatus,2);
		 lambdaUpdateWrapper.set(FwRoomRepair::getCreateTime,new Date());
		 this.fwRoomRepairService.update(lambdaUpdateWrapper);

		 List<String> list = Arrays.asList(roomIds.split(","));
		 for (String s : list) {
			 CesRooms cesRooms = cesRoomsService.getById(s);
			 if (ObjectUtils.isNotEmpty(cesRooms)) {
				 if (cesRooms.getRoomStatus().equals(RoomStatusEnum.IN_SERVICE.getKey())) {
					 cesRooms.setRoomStatus(cesRooms.getLastRoomStatus());
					 cesRoomsService.updateById(cesRooms);
				 }
			 }
		 }
		 return Result.OK("房间完成维修成功!");
	 }

	/**
	 *  编辑
	 *
	 * @param fwRoomRepair
	 * @return
	 */
	@AutoLog(value = "fw_room_repair-编辑")
	@ApiOperation(value="fw_room_repair-编辑", notes="fw_room_repair-编辑")
	//@RequiresPermissions("fw:fw_room_repair:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody FwRoomRepair fwRoomRepair) {
		fwRoomRepairService.updateById(fwRoomRepair);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "fw_room_repair-通过id删除")
	@ApiOperation(value="fw_room_repair-通过id删除", notes="fw_room_repair-通过id删除")
	//@RequiresPermissions("fw:fw_room_repair:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		fwRoomRepairService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "fw_room_repair-批量删除")
	@ApiOperation(value="fw_room_repair-批量删除", notes="fw_room_repair-批量删除")
	//@RequiresPermissions("fw:fw_room_repair:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fwRoomRepairService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "fw_room_repair-通过id查询")
	@ApiOperation(value="fw_room_repair-通过id查询", notes="fw_room_repair-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FwRoomRepair> queryById(@RequestParam(name="id",required=true) String id) {
		FwRoomRepair fwRoomRepair = fwRoomRepairService.getById(id);
		if(fwRoomRepair==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(fwRoomRepair);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param fwRoomRepair
    */
    //@RequiresPermissions("fw:fw_room_repair:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FwRoomRepair fwRoomRepair) {
        return super.exportXls(request, fwRoomRepair, FwRoomRepair.class, "fw_room_repair");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("fw:fw_room_repair:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FwRoomRepair.class);
    }

}
